// pages/home/examine/examineindex/examineindex.js
const globalIp = getApp().globalData.userIp
Page({

  /**
   * 页面的初始数据
   */
  data: {
    arrayIsEmp: ['客户', '员工'],
    indexIsEmp: 0,
    arrayDept: [],
    indexDept: 0,
    arrayLineStatus: ['待审O', '生效Y'],
    indexLineStatus: 0,
    arrayRole: [],
    indexRole: 0,
    RoleName: '',
    PhoneNumber: "",
    CardName: "",
    UserSignName: "",
    UserName: "",
    Openid: "",
    Authority: "",
    Remark: "",
    LineNum: 0,
    tie: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getRole();
    this.getDept();
    this.getProject(options);
    this.setData({
      LineNum: options.LineNum,
      tie:  options.tie
    })
  },

  bindChange1: function(e) {
    this.setData({
      indexIsEmp: e.detail.value
    })
  },

  bindChange2: function(e) {
    this.setData({
      indexLineStatus: e.detail.value
    })
  },

  bindChange3: function(e) {
    this.setData({
      indexRole: e.detail.value
    })
  },

  bindChange4: function(e) {
    this.setData({
      indexDept: e.detail.value
    })
  },

  // 角色列表请求
  getRole: function() {
    let that = this;
    wx.request({
      url: globalIp + 'home/queryRole',
      data: {
        openId: wx.getStorageSync('userInfo').openid || '',
      },
      header: {
        'content-type': 'application/json'
      },
      method: 'POST',
      success: function(res) {
        if (res.data.jsonObject) {
          that.setData({
            arrayRole: res.data.jsonObject
          })
        } 
      },
      fail: function(res) {
        wx.showToast({
          title: '请返回上一页面，重新请求',
          icon: 'none',
          duration: 2000,
        })
      }
    })
  },

  // 部门列表请求
  getDept: function() {
    let that = this;
    wx.request({
      url: globalIp + 'home/showAllDept',
      data: {
        openId: wx.getStorageSync('userInfo').openid || '',
      },
      header: {
        'content-type': 'application/json'
      },
      method: 'POST',
      success: function(res) {
        console.log(res.data);
        if (res.data.success) {
          that.setData({
            arrayDept: res.data.JSONObject.data
          })
        } 
      },
      fail: function(res) {
        wx.showToast({
          title: '请返回上一页面，重新请求',
          icon: 'none',
          duration: 2000,
        })
      }
    })
  },

  // 统一请求
  getProject: function(options) {
    let that = this;
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    var length = 0;
    wx.request({
      url: globalIp + 'home/queryUserIndex',
      data: {
        value: options.LineNum || '',
        openId: wx.getStorageSync('userInfo').openid || '',
      },
      header: {
        'content-type': 'application/json'
      },
      method: 'POST',
      success: function(res) {
        if (res.data.jsonObject) {
          var object = res.data.jsonObject;
          console.log(object)
          that.setData({
            indexIsEmp: object.IsEmp == 'U' ? 0 : 1,
            indexLineStatus: object.LineStatus == 'O' ? 0 : 1,
            UserName: object.UserName,
            PhoneNumber: object.PhoneNumber,
            CardName: object.CardName,
            UserSignName: object.UserSignName,
            Openid: object.Openid,
            indexRole: object.init != 0 ? object.init - 1 : 0,
            RoleName: object.init != 0 ? '已设置' : '未设置',
            Authority: object.Authority,
            Remark: object.Remark,
            indexDept: object.DeptNum
          })
        } 
      },
      fail: function(res) {
        wx.showToast({
          title: '请返回上一页面，重新请求',
          icon: 'none',
          duration: 2000,
        })
      },
      complete: function(res) {
        setTimeout(() => {
          wx.hideLoading()
        }, 15 * length)
      }
    })
  },

  // 提交
  formSubmit(e) {
    let that = this;
    wx.request({
      url: globalIp + 'home/updateExamine',
      data: {
        openId: wx.getStorageSync('userInfo').openid || '',
        isEmp: that.data.indexIsEmp == 0 ? 'U' : 'E',
        lineNum: that.data.indexDept,
        lineStatus: that.data.indexLineStatus == 0 ? 'O' : 'Y',
        userName: e.detail.value.UserName,
        value: that.data.Openid,
        pageSize: that.data.LineNum,
        pageIndex: parseInt(that.data.indexRole) + 1  
      },
      header: {
        'content-type': 'application/json'
      },
      method: 'POST',
      success: function(res) {
        if (res.data.success) {
          wx.showToast({
            title: '完善信息成功！',
            icon: 'success',
            success: function(res) {
              setTimeout(() => {
                // wx.navigateTo({
                //   url: '../examine',
                // })
                wx.navigateBack({
                  delta: 1
                });
              }, 1000)
            }
          })
        }
      },
      fail: function(res) {
        wx.showToast({
          title: '系统有误，请重新提交！',
          icon: 'none',
          duration: 2000
        })
      }
    })
  },

  roleChange: function() {
    wx.navigateTo({
      url: '../examinerole/examinerole?Openid='+this.data.Openid
    })
  }
})